package com.five.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.admin.dto.BannerDto;
import com.five.admin.server.BannerService;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.pojo.dto.BatchIds;
import com.five.pojo.entity.Banner;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Resource
    private BannerService bannerService;

    @PostMapping("/add")
    public Result<?> add(@RequestBody Banner banner) {
        return bannerService.save(banner) ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") Long id) {
        return bannerService.removeById(id) ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @DeleteMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody BatchIds batchIds) {
        return bannerService.removeByIds(batchIds.getIds()) ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<?> update(@RequestBody Banner banner) {
        return bannerService.updateById(banner) ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @PostMapping("/list")
    public Result<?> list(@RequestBody BannerDto bannerDto) {
        System.err.println(bannerDto);
        Page<Banner> page = new Page<>(bannerDto.getPage(), bannerDto.getLimit());
        Page<Banner> resultPage = bannerService.findAll(page, bannerDto);
        return Result.buildSuccess(resultPage.getTotal(), resultPage.getRecords());

    }

    @GetMapping("/findBannerByPosition/{position}")
    public Result<?> findBannerByPosition(@PathVariable Integer position) {
        return Result.buildSuccess(bannerService.findBannerByPosition(position));
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<?> getById(@PathVariable("id") Long id) {
        return Result.buildSuccess(bannerService.getById(id));
    }
}
